import pandas as pd
from datetime import datetime, timedelta
from ics import Calendar, Event

cal = Calendar()

df = pd.read_excel('cal_ics/ics.xlsx', sheet_name='信息部排班')
for _,row in df.iterrows():
    date_value  = row.iloc[0]
    name = row.iloc[1]
    duty = row.iloc[2]
    if isinstance(date_value, datetime):
        event_date = date_value
    elif isinstance(date_value, str):
        try:
            event_date = datetime.strptime(date_value, "%Y/%m/%d")
        except ValueError:
            continue
    else:
        # 尝试转换日期值
        try:
            event_date = date_value.to_pydatetime()
        except AttributeError:
            continue
    event = Event()
    event.name = f'{name} {duty}'
    event.begin = event_date
    event.end = event_date + timedelta(days=1)
    event.description = f'信息部排班: {name} {duty}'

    if '值班' in duty:
        event.categories = ['值班']
        event.color = '#FF0000'
    else:
        event.categories = ['听班']
        event.color = '#00ff00'
    cal.events.add(event)

with open('cal_ics/ics.ics', 'w', encoding='utf-8') as f:
        f.writelines(cal.serialize_iter())
    
print("ICS日历已生成: schedule.ics")
print(f"共生成 {len(cal.events)} 个事件")
